package br.ufrgs.meetapp.activities;

import java.text.SimpleDateFormat;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Html;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TextView;
import br.ufrgs.meetapp.utils.UserPreferencesPersistence;

public class AboutActivity extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
    	
	    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    	setContentView(R.layout.aboutactivity);
    
        //Initialize a TextView to set the text    
	    TextView aboutTextView = (TextView)findViewById(R.id.aboutTextView);
	    aboutTextView.setText(Html.fromHtml(getString(R.string.about_text)));
	    
	    //Create a pattern to recognize only real phone numbers
	    Pattern pattern = Pattern.compile("\\+[0-9][0-9] [(]?[0-9]?[0-9]?[)]?[ ]?[-]?[0-9]+[-]?[0-9]?");
 		String scheme = "tel:";
 		
 		//Add the links
 		Linkify.addLinks(aboutTextView, Linkify.EMAIL_ADDRESSES | Linkify.WEB_URLS);
 		Linkify.addLinks(aboutTextView, pattern, scheme);
 		

 		//TODO: REMOVE THE FOLLOWING CODE. JUST FOR TEST PURPOSES
 		//Initialize a TextView to set the compilation Time    
	    TextView compilationTimeTextView = (TextView)findViewById(R.id.aboutCompilationTimeTextView);
	    try{
		     ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
		     ZipFile zf = new ZipFile(ai.sourceDir);
		     ZipEntry ze = zf.getEntry("classes.dex");
		     long time = ze.getTime();
		     String s = SimpleDateFormat.getInstance().format(new java.util.Date(time));
		     compilationTimeTextView.setText("Compilation time:" + s.toString());
		  }catch(Exception e){
		}
	    //---------------------------------------------------------------------
	    
	    setTitleBar();
 	}
	
	/***
	 * Sets the top bar
	 */
	private void setTitleBar() {
		TextView titleBarTitle;
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
		titleBarTitle = (TextView) findViewById(R.id.title_bar_text_title);
		titleBarTitle.setText(R.string.about_title);
	}
	
	/**
	 * Create the hard-button menu
	 */
	public boolean onCreateOptionsMenu(Menu menu){
    	if(LoginActivity.loggedUser != null){
    		super.onCreateOptionsMenu(menu);
        	MenuInflater menuInflater = getMenuInflater();
        	
        	if(UserPreferencesPersistence.getIsOnlyTrackOthers(this)){
        		menuInflater.inflate(R.menu.menu_account, menu);
        	} else {
        		menuInflater.inflate(R.menu.menu_settings_account, menu);
        	}
        	return true;
    	}
    	else 
    		return false;
		
    }
    
	/**
	 * Actions to the hard-button menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;
		switch (item.getItemId()) {
		case R.id.menuSettings:
			intent = new Intent(this, SettingsActivity.class);
			break;
		}
		
		if(intent!=null){
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		}
		
		return false;
	}
}
